﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using KeRui.Common;
using KeRui.Upload;
using System.IO;


public partial class Manage_Cms_NewSubject : System.Web.UI.Page
{
    ManageErrMsg nsb = new ManageErrMsg();
    public string act = PageRequest.GetString("act");//动作
    public int ID = PageRequest.GetInt("ID", -1);//模板附加字段
    public int FileID = PageRequest.GetInt("FileID",-1);//
    protected int siteid = PageRequest.GetInt("siteid", -1);//站点 

    public KeRui.Model.CmsTemplateFileAttr modelCmsTemplateFileAttr;
    protected string[] templates;//元素字符串

    
    protected KeRui.BLL.CmsNode bllCmsNode = new KeRui.BLL.CmsNode();
    protected KeRui.BLL.CmsTemplateFile bllCmsTemplateFile = new KeRui.BLL.CmsTemplateFile();
    protected KeRui.BLL.CmsTemplateFileAttr bllCmsTemplateFileAttr = new KeRui.BLL.CmsTemplateFileAttr();
    protected KeRui.BLL.CmsTemplateSubjectsAttr bllCmsTemplateSubjectsAttr = new KeRui.BLL.CmsTemplateSubjectsAttr();
    protected KeRui.Model.CmsNode modelCmsNode = new KeRui.Model.CmsNode();
    protected KeRui.Model.CmsSite modelCmsSite = new KeRui.Model.CmsSite();

    //栏目信息
    protected string CommentTemplate = PageRequest.GetString("CommentTemplate");
    protected string ContentDefaultTemplateFile = PageRequest.GetString("ContentDefaultTemplateFile");
    protected string nodename = "";
    protected string nodeindex = "";
    protected string[] notes;
    protected string[] TPs;
    protected string[] Nodes;
    protected string[] Titles;
    protected DataSet dsContentTemp = new DataSet();
    protected DataSet dsCommentTemp = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        KeRui.BLL.CmsSite bllCmsSite = new KeRui.BLL.CmsSite();
        modelCmsSite = bllCmsSite.GetModel(siteid);
        ManageHelper.CheckAdminLogin();//登陆判断

        dsContentTemp = bllCmsTemplateFile.GetSelect(modelCmsSite.TemplateID, 3);//内容模板
        dsCommentTemp = bllCmsTemplateFile.GetSelect1(modelCmsSite.TemplateID, 5);//评论模板

        switch (act)
        {
            case "new":
                act = "add";
                break;
            case "add": 
                upload();
                break;
            case "edit":
                Edit();
                act = "editSave";
                break;
            case "editSave":
                EditSave();
                break;
        }
        if (ID != -1)
        {
            modelCmsTemplateFileAttr = bllCmsTemplateFileAttr.GetModel(ID);
            templates = (modelCmsTemplateFileAttr.Notes).Split(',');
        }

    }
    //生成列表

    public string SelectNodeList(int pId, int intTemp, int nodeID)
    {
        string RetStr = "";
        List<KeRui.Model.CmsNode> list = bllCmsNode.GetListByPId(pId, siteid, nodeID);
        int j = 0;
        foreach (KeRui.Model.CmsNode model in list)
        {
            j++;
            string Empty = "";
            for (int i = 0; i < model.Depth; i++)
            {
                Empty += "　";
            }
            if (model.NodeID == intTemp)
            {
                RetStr += "<option value=" + model.NodeID + " selected>" + Empty + "{0}" + model.NodeName + "</option>";
            }
            else
            {
                RetStr += "<option value=" + model.NodeID + ">" + Empty + "{0}" + model.NodeName + "</option>";
            }
            if (model.ChildNum > 0)
            {
                RetStr += SelectNodeList(model.NodeID, intTemp, nodeID);
            }
            if (j == list.Count)
            {
                RetStr = RetStr.Replace("{0}", "└");
            }
            else
            {
                RetStr = RetStr.Replace("{0}", "├");
            }
        }
        return RetStr.ToString();
    }


    
    public bool Judge(string filePath)
    {

        bool flag = false;
        string ext = GetExt(filePath);
        string Exts = "";
        Exts = ".jpg.gif.jpeg.jpe.png.bmp";
        if (Exts.IndexOf(ext.ToLower()) != -1)
        {
            flag = true;
        }
        return flag;
        
    }

    public string CreatePathWithDate(string strV, string strT)
    {
        string uploadpath = "", s = "";

        //替换strT里的年月日伪字符
        if (strT.IndexOf(":") >= 0)
        {
            string[] strTs = strT.Split(":".ToCharArray());
            for (int i = 0; i < strTs.Length; i++)
            {
                strTs[i] = strTs[i].Replace("YY", DateTime.Now.Year.ToString());
                strTs[i] = strTs[i].Replace("MM", DateTime.Now.Month.ToString());
                strTs[i] = strTs[i].Replace("DD", DateTime.Now.Day.ToString());
                if (i != strTs.Length)
                {
                    uploadpath = uploadpath + strTs[i] + "/";
                }
                else
                {
                    uploadpath = uploadpath + strTs[i];
                }
                DirectoryInfo Fsofolder = new DirectoryInfo(Server.MapPath(strV + uploadpath));
                if (!Fsofolder.Exists)
                {
                    Fsofolder.Create();
                }
            }
            s = strV + uploadpath;
        }
        else
        {
            uploadpath = strT.Replace("YY", DateTime.Now.Year.ToString());
            uploadpath = uploadpath.Replace("MM", DateTime.Now.Month.ToString());
            uploadpath = uploadpath.Replace("DD", DateTime.Now.Day.ToString());
            DirectoryInfo Fsofolder = new DirectoryInfo(Server.MapPath(strV + uploadpath));
            if (!Fsofolder.Exists)
            {
                Fsofolder.Create();
                s = strV + uploadpath + "/";
            }
        }
        return s;
    }
    private string GetExt(string filePath)
    {
        return filePath.Substring(filePath.LastIndexOf(".")); 
    }

    private void Edit()//修改专题   
    {
        //获得专题栏目信息
        KeRui.Model.CmsTemplateSubjectsAttr ModelCmsTemplateSubjectsAttr = new KeRui.Model.CmsTemplateSubjectsAttr();
        List<KeRui.Model.CmsTemplateSubjectsAttr> listCmsTemplateSubjectsAttr=bllCmsTemplateSubjectsAttr.GetList(" FileID='" + FileID + "'");
        if (listCmsTemplateSubjectsAttr.Count != 0)
        {
            ModelCmsTemplateSubjectsAttr = listCmsTemplateSubjectsAttr[0];
        }
        else
        {
            Response.Write("<script type=\"text/javascript\">alert('没有找到相应的模板信息，请检查数据库数据的完整性！');</script>");
            Response.End();
        }
        
        //KeRui.Model.CmsNode ModelCmsNode = new KeRui.Model.CmsNode();
        modelCmsNode = bllCmsNode.GetModel(ModelCmsTemplateSubjectsAttr.NodeID);//栏目
        nodename = modelCmsNode.NodeName;
        nodeindex = modelCmsNode.NodeKey.Replace("Subjects_", "");
        //获取专题模板信息
        KeRui.Model.CmsTemplateFile ModelCmsTemplateFile= new KeRui.Model.CmsTemplateFile();
        ModelCmsTemplateFile = bllCmsTemplateFile.GetModel(FileID);
        notes = ModelCmsTemplateSubjectsAttr.Notes.Split('_');
        TPs = notes[0].Split(',');
        Nodes = notes[1].Split(',');
        Titles = notes[2].Split(',');
        ID = ModelCmsTemplateSubjectsAttr.AttrID;
        //获得评论模板信息

    }
    public void upload()
    {
        Upload upload = new Upload();
        string tPath = "";
        for (int i = 0; i < upload.Files.Count; i++)
        {
            //上传图片，获得图片路径，存储在HTML
            UploadedFile upfile = upload.Files[i];
            if (upfile.FileName == "")
            {
                nsb.AddErr("Error:请选择图片"); nsb.ChkErr();
            }
            else
            {
                string tpPath = upfile.FileName;
                if (Judge(tpPath))
                {
                    string fileExt = tpPath;
                    string Path = CreatePathWithDate("/Upload/", "YY-MM:DD");
                    tpPath = Path + System.Guid.NewGuid() + GetExt(upfile.Extension);
                    upfile.SaveAs(Server.MapPath(tpPath));
                    tPath = tPath + tpPath + ",";
                }
                else
                {

                    nsb.AddErr("Error:文件格式不正确。请选择一张图片"); nsb.ChkErr();
                }
            }

        }
        string s = PageRequest.GetString("selectList");
        string IndexName = PageRequest.GetString("IndexName");
        string ColumnName = PageRequest.GetString("ColumnName");
        string xwtitle = PageRequest.GetString("xwtitle");
        Response.Redirect("SubjectCreate.aspx?ID=" + ID + "&act=add&Nodeids=" + s + "&siteid=" + siteid + "&fileid=" + FileID + "&IndexName=" + IndexName + "&ColumnName=" + ColumnName + "&tPath=" + tPath + "&xwtitle=" + xwtitle + "&CommentTemplate=" + CommentTemplate + "&ContentDefaultTemplateFile=" + ContentDefaultTemplateFile);
    }
    protected void EditSave()
    {
        KeRui.Model.CmsTemplateSubjectsAttr ModelCmsTemplateSubjectsAttr = new KeRui.Model.CmsTemplateSubjectsAttr();
        ModelCmsTemplateSubjectsAttr = bllCmsTemplateSubjectsAttr.GetList(" FileID='" + FileID + "'")[0];

        notes = ModelCmsTemplateSubjectsAttr.Notes.Split(',');
        Upload upload = new Upload();
        string tPath = "";
        for (int i = 0; i < upload.Files.Count; i++)
        {
            //上传图片，获得图片路径，存储在HTML
            UploadedFile upfile = upload.Files[i];
            string tpPath = upfile.FileName;

            if (upfile.FileName != "")
            {
                if (Judge(tpPath))
                {
                    string fileExt = tpPath;
                    string Path = CreatePathWithDate("/Upload/", "YY-MM:DD");
                    tpPath = Path + System.Guid.NewGuid() + GetExt(upfile.Extension);
                    upfile.SaveAs(Server.MapPath(tpPath));
                    tPath = tPath + tpPath + ",";  
                }
                else
                {
                    nsb.AddErr("Error:文件格式不正确。请选择一张图片"); nsb.ChkErr();
                }
            }
            else
            {
                tPath = tPath + notes[i] + ",";
            }
        }
        string s = PageRequest.GetString("selectList");
        string IndexName = PageRequest.GetString("IndexNamehidden");
        string ColumnName = PageRequest.GetString("ColumnNamehidden");
        string xwtitle = PageRequest.GetString("xwtitle");
        Response.Redirect("SubjectCreate.aspx?ID=" + ID + "&act=editsave&Nodeids=" + s + "&siteid=" + siteid + "&fileid=" + FileID + "&tPath=" + tPath + "&ColumnName=" + ColumnName + "&xwtitle=" + xwtitle + "&CommentTemplate=" + CommentTemplate + "&ContentDefaultTemplateFile=" + ContentDefaultTemplateFile);
       
    }
}
